package com.zhangtai.modules.controller.member;

import cn.hutool.extra.servlet.ServletUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.Neusoft.dto.SalaryDto;
import com.zhangtai.Neusoft.service.NeuEmployeeSalaryService;
import com.zhangtai.common.annotation.SysAuthorityLog;
import com.zhangtai.exception.QwMsg;
import com.zhangtai.exception.sendQwMsgEntity;
import com.zhangtai.modules.constVo.NoticeConst;
import com.zhangtai.modules.controller.admin.DimissionController;
import com.zhangtai.modules.controller.admin.JobController;
import com.zhangtai.modules.controller.aouthLogin.MemberLoginController;
import com.zhangtai.modules.dao.*;
//import com.zhangtai.modules.dao.ssqlDao.EmployeeBaseDao;
import com.zhangtai.modules.dto.SalaryChangeDto;
import com.zhangtai.modules.dto.UserBankCardAnnalDto;
import com.zhangtai.modules.dto.UserBankCardDto;
import com.zhangtai.modules.entity.common.ImgUrlEntity;
import com.zhangtai.modules.entity.employee.EmployeeInfoEntity;
import com.zhangtai.modules.entity.salary.ComputerSubsidyEntity;
import com.zhangtai.modules.entity.salary.SecondaryPasswordEntity;
import com.zhangtai.modules.entity.salary.UserBankCardEntity;
import com.zhangtai.modules.entity.system.AuthorityLogEntity;
import com.zhangtai.modules.jobTask.qw.QwUserEntity;
import com.zhangtai.modules.service.common.ImgUrlService;
import com.zhangtai.modules.service.employee.EmployeeInfoService;
import com.zhangtai.modules.service.salary.ComputerSubsidyService;
import com.zhangtai.modules.service.salary.UserBankCardService;
import com.zhangtai.modules.service.system.AuthorityLogService;
import com.zhangtai.modules.vo.ComputerSubsidyVo;
import com.zhangtai.modules.vo.UserBankCardVo;
import com.zhangtai.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@Api(tags="薪酬信息自助端")
@RequestMapping("/member")
@Slf4j
public class SalaryMemberController {

    @Autowired
    private UserBankCardService userBankCardService;
    @Autowired
    private UserBankCardDao userBankCardDao;

    @Autowired
    private MemberLoginController memberLoginController;
    @Autowired
    private EmployeeInfoService employeeInfoService;
    @Autowired
    private AuthorityLogService authorityLogService;
    @Autowired
    private AuthorityLogDao authorityLogDao;
    @Autowired
    private ImgUrlDao imgUrlDao;
    @Autowired
    private ImgUrlService imgUrlService;
    @Autowired
    private QwMsg qwMsg;
    @Autowired
    private SecondaryPasswordDao secondaryPasswordDao;
    @Autowired
    PasswordEncoder passwordEncoder;
//    @Autowired
//    private EmployeeBaseDao employeeBaseDao;

    @Autowired
    private NeuEmployeeSalaryService neuEmployeeSalaryService;

    @Autowired
    private SalaryChangeDao salaryChangeDao;
    @Autowired
    private JobController jobController;
    @Autowired
    private QwUserDao qwUserDao;
    @Autowired
    private ComputerSubsidyService computerSubsidyService;
    @Autowired
    private ComputerSubsidyDao computerSubsidyDao;
    @Autowired
    private DimissionController dimissionController;
    /**
     * 列表
     */
    @GetMapping("/getUserBankCardInfo")
    @ApiOperation(value="查询员工工资卡信息")
    //  @PreAuthorize("hasAuthority('emp:socialSecurity:get')")
    public R getUserBankCardInfo( HttpServletRequest request) throws Exception {
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        if (ObjectUtils.isEmpty(data)) {
            return  R.error("请先登录");
        }
        UserBankCardEntity userBankCardEntity = userBankCardDao.selectOne(new QueryWrapper<UserBankCardEntity>()
                .eq("oid", data.getLong("id")).eq("is_delete", 0));
        UserBankCardDto userBankCardDto= BeanUtilsMabach.doToDto(userBankCardEntity, UserBankCardDto.class);
        if(userBankCardDto!=null){
          EmployeeInfoEntity employeeInfoEntity= employeeInfoService.getByIdE( data.getLong("id"));
            userBankCardDto.setName(employeeInfoEntity.getName());
           List<AuthorityLogEntity> authorityLogEntityList=authorityLogDao.selectList(new QueryWrapper<AuthorityLogEntity>()
                    .eq("oid",data.getLong("id"))
                    .eq("is_delete", 0)
                    .eq("type", 3)
            );
            List<UserBankCardAnnalDto> UserBankCardAnnalDtoList=new ArrayList<>();
          if( !CollectionUtils.isEmpty(authorityLogEntityList)){
                for (AuthorityLogEntity authorityLogEntity:authorityLogEntityList){
                    UserBankCardEntity userBankCard= JSONObject.parseObject(authorityLogEntity.getParams(), UserBankCardEntity.class);
                    UserBankCardAnnalDto userBankCardAnnalDto= BeanUtilsMabach.doToDto(userBankCard, UserBankCardAnnalDto.class);
                    userBankCardAnnalDto.setInvalidDate(authorityLogEntity.getUpdateTime());
                    UserBankCardAnnalDtoList.add(userBankCardAnnalDto);
                }
          }
            userBankCardDto.setUserBankCardAnnalDtoList(UserBankCardAnnalDtoList);
        }
        return R.ok(userBankCardDto);
    }

    @PostMapping("/updateUserBankCard")
    @ApiOperation(value="修改/新增员工工资卡信息")
    @SysAuthorityLog(value="修改/新增员工工资卡信息")
    @Transactional
    //  @PreAuthorize("hasAuthority('emp:socialSecurity:get')")
    public R updateUserBankCard(@RequestBody @Valid UserBankCardVo userBankCardVo,
                                HttpServletRequest request) throws Exception {
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        if (ObjectUtils.isEmpty(data)) {
            return  R.error("请先登录");
        }
        Long oid= data.getLong("id");
        if(userBankCardVo.getId()==null) {
            List<UserBankCardEntity> userBankCardList = userBankCardDao.selectList(new QueryWrapper<UserBankCardEntity>()
                    .eq("oid",oid).eq("is_delete", 0));
            if(!CollectionUtils.isEmpty(userBankCardList)){
                return R.error("您已存在工资卡信息，无需添加");
            }
        }
            UserBankCardEntity userBankCardEntity= BeanUtilsMabach.doToDto(userBankCardVo,UserBankCardEntity.class);
       if(!StringUtils.isEmpty(userBankCardEntity.getIdcardUpUrl()))
           saveFile(data.getLong("id"),userBankCardEntity.getIdcardUpUrl(),"银行卡正面",21);
        if(!StringUtils.isEmpty(userBankCardEntity.getIdcardDownUrl()))
            saveFile(data.getLong("id"),userBankCardEntity.getIdcardDownUrl(),"银行卡反面",21);
        userBankCardEntity.setOid(oid);
        userBankCardEntity.setStatus(1);
        userBankCardEntity.setUpdateTime(new Date());
        userBankCardEntity.setUpdateCode(data.getString("usercode"));
        userBankCardEntity.setUpdateName(data.getString("name"));
        userBankCardEntity.setEffectiveDate(new Date());
        if(userBankCardEntity.getId()==null){
            userBankCardEntity.setIsDelete(0);
            userBankCardEntity.setType(1);
            userBankCardEntity.setCreateTime(new Date());
//            userBankCardEntity.setOrgId(Long.valueOf(data.getString("orgId")));
            userBankCardEntity.setUnitId(data.getLong("depId"));
            userBankCardEntity.setCreateCode(data.getString("usercode"));
            userBankCardEntity.setCreateName(data.getString("name"));
            userBankCardService.saveE(userBankCardEntity);
        }else{
            userBankCardEntity.setType(2);
            userBankCardService.updateByIdE(userBankCardEntity);
        }
        //记录变更
        AuthorityLogEntity authorityLogEntity=new AuthorityLogEntity();
        authorityLogEntity.setJobId(data.getLong("jobId"));
        authorityLogEntity.setJobName(data.getString("jobname"));
        authorityLogEntity.setUpdateUserName(data.getString("name"));
        authorityLogEntity.setUsercode(data.getString("usercode"));
        authorityLogEntity.setIp(ServletUtil.getClientIP(request));
        authorityLogEntity.setOid(data.getLong("id"));
        authorityLogEntity.setType(3);
        //Dept dept= JSONObject.parseObject(response, Dept.class);
        authorityLogEntity.setParams(JSONObject.toJSONString(userBankCardEntity));
        authorityLogEntity.setIsDelete(0);
        authorityLogEntity.setUpdateTime(new Date());
        authorityLogEntity.setRemark("员工工资卡变更记录");
        authorityLogService.saveE(authorityLogEntity);
        String content = "您有一位员工工资卡确认生效："+data.getString("name");
        qwMsg.sendNoticeMsg("工资卡异动",content, NoticeConst.TODO_TYPE_CARD_CHANGE,false,data.getLong("id"),userBankCardEntity.getId(),"工资卡异动",null,new Date(),data.getLong("orgId"));
        return R.ok();
    }

    //附件保存
    public void saveFile(Long oid, String fileUrl,String name,Integer type){
            imgUrlDao.delete(new QueryWrapper<ImgUrlEntity>().eq("url", fileUrl).eq("type", type));
            ImgUrlEntity imgUrlEntity = new ImgUrlEntity();
            imgUrlEntity.setName(name);
            imgUrlEntity.setOid(oid);
            imgUrlEntity.setType(type);
            imgUrlEntity.setCreateTime(new Date());
            imgUrlEntity.setUrl(fileUrl);
            imgUrlService.saveE(imgUrlEntity);
    }


    @PostMapping("/getEmployeeSalary")
    @ApiOperation("个人工资")
    public R getEmployeeSalary(HttpServletRequest request,@RequestParam(value = "yymm") String yymm,
                               @RequestParam(value = "endyymm") String endyymm, @RequestParam(value = "authToken") String authToken) throws Exception {
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        if (ObjectUtils.isEmpty(data)) {
            return  R.error("请先登录");
        }
        String code= data.getString("usercode");
        //校验模块二次密码----
        R r=checkAuthToken(authToken,request);
        if(!r.getFlag()) return r;

        JSONObject userInfo = memberLoginController.getUserInfo(request).getData();
        return neuEmployeeSalaryService.getEmployeeWage(code,yymm,endyymm);
    }
    @GetMapping("/getEmployeeSalaryModule")
    @ApiOperation("个人工资模块信息")
    public R getEmployeeSalaryModule(@RequestParam(value = "authToken") String authToken, HttpServletRequest request) throws Exception {
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        if (ObjectUtils.isEmpty(data)) {
            return  R.error("请先登录");
        }
        String code= data.getString("usercode");
        //校验模块二次密码----
        R r=checkAuthToken(authToken,request);
        if(!r.getFlag()) return r;

       // String bbeginDate= DateUtils.formatYM(new Date())+"-01";
        List<SalaryDto> salaryDtoList= new ArrayList<>();
//        employeeBaseDao.getEmployeeSalaryList(code,null);
        return R.ok(salaryDtoList);
    }

    @GetMapping("/getSalaryChangeList")
    @ApiOperation("查询员工薪资调整信息")
    public R getSalaryChangeList(@RequestParam(value = "authToken") String authToken,
                                 @RequestParam(value = "dateStart",required = false) String dateStart,
                                 @RequestParam(value = "dateEnd",required = false) String dateEnd,
                                 @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                 HttpServletRequest request)throws Exception{

        JSONObject data = memberLoginController.getUserInfo(request).getData();
        if (ObjectUtils.isEmpty(data)) {
            return  R.error("请先登录");
        }
        String oid= data.getString("id");
        //校验模块二次密码----
        R r=checkAuthToken(authToken,request);
        if(!r.getFlag()) return r;

        Page<SalaryChangeDto> page = new Page<SalaryChangeDto>(pageNum,pageSize);
        List<SalaryChangeDto> salaryChangeList=  salaryChangeDao.getSalaryChangePage(oid,dateStart,dateEnd,page);
        for (SalaryChangeDto salaryChangeDto : salaryChangeList) {
            if (salaryChangeDto.getJobId() != null && StringUtils.isEmpty(salaryChangeDto.getJobname()))
                salaryChangeDto.setJobname(jobController.getJobName(salaryChangeDto.getJobId()));
            if (salaryChangeDto.getOrgId() != null && StringUtils.isEmpty(salaryChangeDto.getOrgname()))
                salaryChangeDto.setOrgname(jobController.getOrgNameById(salaryChangeDto.getOrgId()));
            if (salaryChangeDto.getUnitId() != null && StringUtils.isEmpty(salaryChangeDto.getUnitname()))
                salaryChangeDto.setUnitname(jobController.getOrgNameById(salaryChangeDto.getUnitId()));

            if (salaryChangeDto.getNewJobId() != null && StringUtils.isEmpty(salaryChangeDto.getNewJobName()))
                salaryChangeDto.setNewJobName(jobController.getJobName(salaryChangeDto.getNewJobId()));
            if (salaryChangeDto.getNewOrgId() != null && StringUtils.isEmpty(salaryChangeDto.getNewOrgName()))
                salaryChangeDto.setNewOrgName(jobController.getOrgNameById(salaryChangeDto.getNewOrgId()));
            if (salaryChangeDto.getNewUnitId() != null && StringUtils.isEmpty(salaryChangeDto.getNewUnitName()))
                salaryChangeDto.setNewUnitName(jobController.getOrgNameById(salaryChangeDto.getNewUnitId()));
        }
        page.setRecords(salaryChangeList);
        return R.ok(new PageResult<SalaryChangeDto>(page));
    }



    @PostMapping("/savePwd")
    @ApiOperation(value="新增二次密码")
    @SysAuthorityLog(value="新增二次密码")
    @Transactional
    public R savePwd(@RequestParam(value = "pwd") String pwd,HttpServletRequest request) throws Exception {
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        if (ObjectUtils.isEmpty(data)) {
            return  R.error("请先登录");
        }
       Long oid= data.getLong("id");
        String uuid = UUID.randomUUID().toString().replace("-", "");
        SecondaryPasswordEntity secondaryPasswordEntity= secondaryPasswordDao.selectOne(new QueryWrapper<SecondaryPasswordEntity>().eq("oid",oid));
        if(ObjectUtils.isEmpty(secondaryPasswordEntity)){
            secondaryPasswordEntity=new SecondaryPasswordEntity();
            secondaryPasswordEntity.setEffectiveTime(new Date());
            secondaryPasswordEntity.setOid(Long.valueOf(oid));
            secondaryPasswordEntity.setPwd(passwordEncoder.encode(pwd));
            secondaryPasswordEntity.setAuthToken(uuid);
            secondaryPasswordDao.insert(secondaryPasswordEntity);
        }else {
            return R.error("二次密码已存在，无需新增！");
        }
        return R.ok(uuid);
    }


    @PostMapping("/updatePwd")
    @ApiOperation(value="根据验证码修改二次密码")
    @SysAuthorityLog(value="根据验证码修改二次密码")
    @Transactional
    public R updatePwd(@RequestParam(value = "pwd") String pwd,HttpServletRequest request,@RequestParam(value = "checkCode",required = false) String checkCode) throws Exception {
        //暂时关闭修改密码验证码
        boolean isQd=false;
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        if (ObjectUtils.isEmpty(data)) {
            return  R.error("请先登录");
        }
        Long oid= data.getLong("id");
        String uuid = UUID.randomUUID().toString().replace("-", "");
        SecondaryPasswordEntity secondaryPasswordEntity= secondaryPasswordDao.selectOne(new QueryWrapper<SecondaryPasswordEntity>().eq("oid",oid));
        if(ObjectUtils.isEmpty(secondaryPasswordEntity)){
            return R.error("noEntity");
        }else{
            if(isQd){
                if(!checkCode.equals(secondaryPasswordEntity.getCheckCode()))return R.error("验证码输入错误！");
            }
            secondaryPasswordEntity.setCheckCode(" ");
            secondaryPasswordEntity.setEffectiveTime(new Date());
            secondaryPasswordEntity.setPwd(passwordEncoder.encode(pwd));
            secondaryPasswordEntity.setAuthToken(uuid);
            secondaryPasswordDao.updateById(secondaryPasswordEntity);
        }
        return R.ok(uuid);
    }

    @PostMapping("/getCheckCode")
    @ApiOperation(value="获取验证码  发送到企业微信")
    @Transactional
    public R getCheckCode(HttpServletRequest request) throws Exception {
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        if (ObjectUtils.isEmpty(data)) {
            return  R.error("请先登录");
        }
        Long oid= data.getLong("id");
        String code= data.getString("usercode");
        SecondaryPasswordEntity secondaryPasswordEntity= secondaryPasswordDao.selectOne(new QueryWrapper<SecondaryPasswordEntity>().eq("oid",oid));
        if(ObjectUtils.isEmpty(secondaryPasswordEntity)){
            return R.error("noEntity");
        }
        List<QwUserEntity> qwUserEntitys = qwUserDao.selectList(new QueryWrapper<QwUserEntity>().eq("user_code", code));
        if (!CollectionUtils.isEmpty(qwUserEntitys)){
            QwUserEntity qwUserEntity = qwUserEntitys.get(0);
            int max=999999,min=99999;
            int ran2 = (int) (Math.random()*(max-min)+min);
            qwMsg.sendNoticeMsg(new sendQwMsgEntity("【员工自助端】验证码",
                    ran2+",用于验证自助端二次码密码验证，泄露有风险，如非本人操作，请忽略本条信息。",
                    qwUserEntity.getUserid()));
            secondaryPasswordEntity.setCheckCode(ran2+"");
        }else {
            return R.error("你还未绑定企业微信,请联系管理员！");
        }
        return R.ok();
    }


    @PostMapping("/checkPwd")
    @ApiOperation(value="校验二次密码")
    @Transactional
    public R checkPwd(@RequestParam(value = "pwd") String pwd,HttpServletRequest request) throws Exception {
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        if (ObjectUtils.isEmpty(data)) {
            return  R.error("请先登录");
        }
        Long oid= data.getLong("id");
        String uuid = UUID.randomUUID().toString().replace("-", "");
        SecondaryPasswordEntity secondaryPasswordEntity= secondaryPasswordDao.selectOne(new QueryWrapper<SecondaryPasswordEntity>().eq("oid",oid));
        if(ObjectUtils.isEmpty(secondaryPasswordEntity)){
            return R.error("noEntity");
        }
        if(!passwordEncoder.matches(pwd,secondaryPasswordEntity.getPwd())){
            return R.error("二次密码输入错误！");
        }
        secondaryPasswordEntity.setAuthToken(uuid);
        secondaryPasswordEntity.setEffectiveTime(new Date());
        secondaryPasswordDao.updateById(secondaryPasswordEntity);
        return R.ok(uuid);
    }

    @PostMapping("/updatePwdByOldPwd")
    @ApiOperation(value="根据原二次密码修改二次密码")
    @SysAuthorityLog(value="根据原二次密码修改二次密码")
    @Transactional
    public R updatePwd(@RequestParam(value = "oldPwd") String oldPwd,@RequestParam(value = "newPwd") String newPwd,HttpServletRequest request) throws Exception {
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        if (ObjectUtils.isEmpty(data)) {
            return  R.error("请先登录");
        }
        Long oid= data.getLong("id");
        String uuid = UUID.randomUUID().toString().replace("-", "");
        SecondaryPasswordEntity secondaryPasswordEntity= secondaryPasswordDao.selectOne(new QueryWrapper<SecondaryPasswordEntity>().eq("oid",oid));
        if(ObjectUtils.isEmpty(secondaryPasswordEntity)){
            return R.error("noEntity");
        }
        if(!passwordEncoder.matches(oldPwd,secondaryPasswordEntity.getPwd())){
            return R.error("原二次密码输入错误！");
        }
        secondaryPasswordEntity.setEffectiveTime(new Date());
        secondaryPasswordEntity.setPwd(passwordEncoder.encode(newPwd));
        secondaryPasswordEntity.setAuthToken(uuid);
        secondaryPasswordDao.updateById(secondaryPasswordEntity);
        return R.ok(uuid);
    }



    @PostMapping("/checkAuthToken")
    @ApiOperation(value="校验AuthToken")
    @Transactional
    public R checkAuthToken(@RequestParam(value = "authToken") String authToken,HttpServletRequest request) throws Exception {
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        if (ObjectUtils.isEmpty(data)) {
            return  R.error("请先登录");
        }
        Long oid= data.getLong("id");
       SecondaryPasswordEntity secondaryPasswordEntity= secondaryPasswordDao.selectOne(new QueryWrapper<SecondaryPasswordEntity>().eq("oid",oid));
       if(ObjectUtils.isEmpty(secondaryPasswordEntity)){
           return R.error("noEntity");
       }
       if(!secondaryPasswordEntity.getAuthToken().equals(authToken)){
           return R.error("enterPwd");
       }
       secondaryPasswordEntity= secondaryPasswordDao.selectOne(new QueryWrapper<SecondaryPasswordEntity>().eq("oid",oid).ge("effective_time", DateUtils.addDateMinutes(new Date(),-15)));
        if(ObjectUtils.isEmpty(secondaryPasswordEntity)){
            return R.error("enterPwd");
        }
        secondaryPasswordEntity.setCheckCode(" ");
        secondaryPasswordDao.updateById(secondaryPasswordEntity);
        return R.ok();
    }



    @GetMapping("/getComputerSubsidyPage")
    @ApiOperation("分页获取个人电脑补贴列表")
    public R getComputerSubsidyPage( @RequestParam(value = "approveStatus",required = false) List<Integer> approveStatus,
                                     @RequestParam(value = "type", required = false) List<Integer> type,
                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                           HttpServletRequest request)throws Exception{
        JSONObject userInfo = memberLoginController.getUserInfo(request).getData();
        if(org.apache.commons.lang3.ObjectUtils.isEmpty(userInfo)){
            return R.error("请登录");
        }
        Long oid = userInfo.getLong("id");
        List<Integer> equipment = new ArrayList<>();
        equipment.add(1);
        PageResult result = computerSubsidyService.queryPage(oid.toString(),equipment,null,null,null,null,null,null,null,null,approveStatus,type,null,pageNum,pageSize);
        return R.ok(result);
    }

    @PostMapping("/saveAndUpdateComputerSubsidy")
    @ApiOperation("保存或修改电脑补贴")
//    @SysAuthorityLog("保存或修改电脑补贴")
    @Transactional
    public R saveAndUpdateComputerSubsidy(@RequestBody @Valid ComputerSubsidyVo computerSubsidyVo,HttpServletRequest request) throws Exception {
        JSONObject userInfo = memberLoginController.getUserInfo(request).getData();
        if(org.apache.commons.lang3.ObjectUtils.isEmpty(userInfo)){
            return R.error("请登录");
        }
        Long oid = userInfo.getLong("id");
        ComputerSubsidyEntity computerSubsidyEntity=BeanUtilsMabach.doToDto(computerSubsidyVo, ComputerSubsidyEntity.class);
        R r;
        if(!CollectionUtils.isEmpty(computerSubsidyVo.getFileUrlList())) {
          dimissionController.saveFile(oid, computerSubsidyVo.getFileUrlList(), "发票附件",8);
        }
        computerSubsidyEntity.setFileUrl(CollectionUtils.isEmpty(computerSubsidyVo.getFileUrlList())?"":dimissionController.ListToString(computerSubsidyVo.getFileUrlList()));
        if(computerSubsidyVo.getId()==null){
            EmployeeInfoEntity employeeInfoEntity= employeeInfoService.getByIdE(oid);
            computerSubsidyEntity.setOid(oid);
            computerSubsidyEntity.setEquipment(1);
            computerSubsidyEntity.setName(employeeInfoEntity.getName());
            computerSubsidyEntity.setCode(employeeInfoEntity.getCode());
            computerSubsidyEntity.setApplyName(employeeInfoEntity.getName());
            computerSubsidyEntity.setApplyCode(employeeInfoEntity.getCode());
            computerSubsidyEntity.setOrgId(employeeInfoEntity.getOrgId());
            computerSubsidyEntity.setUnitId(employeeInfoEntity.getUnitId());
            computerSubsidyEntity.setJobId(employeeInfoEntity.getJobId());
            computerSubsidyEntity.setJobName(jobController.getJobName(employeeInfoEntity.getJobId()));
            computerSubsidyEntity.setOrgName(jobController.getOrgNameById(employeeInfoEntity.getOrgId()));
            computerSubsidyEntity.setUnitName(jobController.getOrgNameById(employeeInfoEntity.getUnitId()));
            computerSubsidyEntity.setApplyDate(new Date());
           r= computerSubsidyService.saveE(computerSubsidyEntity);
        }else{
           r= computerSubsidyService.updateByIdE(computerSubsidyEntity);
        }
        return r;
    }
    @DeleteMapping("/daleteComputerSubsidy")
    @ApiOperation("删除电脑补贴")
//    @SysAuthorityLog("删除电脑补贴")
    @Transactional
    public R daleteComputerSubsidy(@RequestParam (value = "id") String id){
         computerSubsidyDao.deleteById(id);
         return R.ok();
    }

    @GetMapping("/getComputerSubsidyById")
    @ApiOperation("根据id查看电脑补贴")
    public R getComputerSubsidyById(@RequestParam (value = "id") String id){
        ComputerSubsidyEntity computerSubsidyEntity= computerSubsidyService.getByIdE( Long.valueOf(id));
        return R.ok(computerSubsidyEntity);
    }

    @GetMapping("/getComputerSubsidyEndDate")
    @ApiOperation("查询补贴的最后结束日期")
    public R getComputerSubsidyEndDate(@RequestParam(value = "type") Integer type,HttpServletRequest request) throws Exception {
        JSONObject userInfo = memberLoginController.getUserInfo(request).getData();
        if(org.apache.commons.lang3.ObjectUtils.isEmpty(userInfo)){
            return R.error("请登录");
        }
        Date date=null;
        Long oid = userInfo.getLong("id");
        List<ComputerSubsidyEntity>   computerSubsidyEntityList= computerSubsidyDao.selectList(new QueryWrapper<ComputerSubsidyEntity>()
                .eq("oid",oid)
                .eq("type",type)
                .eq("is_delete", 0)
                .orderByDesc("end_date"));
        if(!CollectionUtils.isEmpty(computerSubsidyEntityList)){
            date=computerSubsidyEntityList.get(0).getEndDate();
        }
        return R.ok(date);
    }


}
